import proj4 from "proj4"
const wgs84 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs  ';
const cgcs2000 = '+proj=tmerc +lat_0=0 +lon_0=108 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs';


/* 
 *	cgcs2000 转 经纬度
 */
export function handleCgcs2000ToCoordinate(item) {
	return proj4(cgcs2000, wgs84, item)
}
/* 
 *	经纬度 转 cgcs2000
 */
export function handleCoordinateToCgcs2000(item) {
	return proj4(wgs84, cgcs2000, item)
}


/* 
 *	热力图数据转换
 *
 *  将数据转换为地图 Feature 数据
 */
export function handleThermalMapData(oldData) {
	if (!oldData) return
	let nowTime = new Date().getTime()
	let featureArray = []
	for (var i = 0; i < oldData.length; i++) {
		featureArray.push({
			"type": "Feature",
			"properties": {
				"id": oldData[i].id || nowTime + '',
				"mag": oldData[i].num || 0,
				"felt": null,
				"tsunami": 0
			},
			"geometry": {
				"type": "Point",
				"coordinates": [oldData[i].lng, oldData[i].lat]
			}
		})
	}
	return {
		"type": "FeatureCollection",
		"crs": {
			"type": "name",
			"properties": {
				"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
			}
		},
		"features": featureArray,
	}
}


/* 
 *	流动管道数据转换
 */
export function handleFlowPipelineData(oldData) {
	if (!oldData || oldData.length == 0) return
	let p = oldData.length
	let pathsArray = []
	for (var i = 0; i < p; i++) {
		let isHad = false
		for (var k = 0; k < pathsArray.length; k++) {
			//如果第二条起始 stnod 等于 前一条的 ednod ，表明是同一条线段里面的数据
			if (pathsArray[k].properties.ednod == oldData[i].attributes.stnod) {
				isHad = true
				pathsArray[k].properties.ednod = oldData[i].attributes.ednod

				oldData[i].geometry.paths[0].map(pathItem => {
					//去重后保留
					let tempLngLat = handleCgcs2000ToCoordinate(pathItem)
					let hadRepeat = false
					for (var p = 0; p < pathsArray.length; p++) {
						let tempArray = pathsArray[p].geometry.coordinates
						for (var r = 0; r < tempArray.length; r++) {
							if (tempArray[r] == tempLngLat) {
								hadRepeat = true
								pathsArray[p].geometry.coordinates.push(tempLngLat)
								break
							}
						}
						if (hadRepeat) {
							break
						}
					}
					if (!hadRepeat) {
						pathsArray[k].geometry.coordinates.push(tempLngLat)
					}
				})
			}
		}
		if (!isHad) {
			let pathCoordinateTemp = []
			oldData[i].geometry.paths.map((item) => {
				item.map((tempitem) => {
					pathCoordinateTemp.push(handleCgcs2000ToCoordinate(tempitem))
				})
			})
			pathsArray.push({
				type: "Feature",
				properties: oldData[i].attributes
					// { 
					// ednod: oldData[i].attributes.ednod,
					// stnod: oldData[i].attributes.stnod,
					// gid: oldData[i].attributes.gid,
					// roadName: oldData[i].attributes['所在道路'],
					// companyName: oldData[i].attributes['所属公司']
					// }
					,
				geometry: {
					type: "LineString",
					coordinates: pathCoordinateTemp
				}
			})
		}
	}

	//如果前一条线段的结束是后一条线段的开始，那么就合并线段
	for (var i = 0; i < pathsArray.length; i++) {
		if (i + 1 < pathsArray.length) {
			let item = pathsArray[i].geometry.coordinates
			let lastItem = pathsArray[i + 1].geometry.coordinates
			if (item[item.length - 1] == lastItem[0]) {
				pathsArray[i].geometry.coordinates = pathsArray[i].geometry.coordinates.concat(lastItem);
				++i;
			}
			// //简化线条，只保留固定的坐标数量
			// pathsArray[i].geometry.coordinates = [
			// 	pathsArray[i].geometry.coordinates[0],
			// 	pathsArray[i].geometry.coordinates[pathsArray[i].geometry.coordinates.length - 1]
			// ]
		}
	}

	let roadNameArray = [] //保存线路起点对应的路名

	let allPaths = []
	let result = []
	let maxTime = 70 //动画重置时间 
	for (var i = 0; i < pathsArray.length; i++) {
		if (!pathsArray[i]) {
			pathsArray.splice(i, 1);
			--i;
		}
		let itemCoordinates = pathsArray[i].geometry.coordinates
		let startTime = 0 //动画开始时间
		let temptimestamps = []
		itemCoordinates.map((item) => {
			startTime += 30
			temptimestamps.push(startTime)
			maxTime = startTime > maxTime ? startTime : maxTime
		})
		result.push({
			"vendor": 0,
			"path": itemCoordinates,
			"timestamps": temptimestamps
		})
		//记录每条线路中间点位、路名，用于显示线条名称
		let centerPoint = []
		if (itemCoordinates.length > 2) {
			centerPoint = itemCoordinates[parseInt(itemCoordinates.length / 2)]
		} else {
			centerPoint = itemCoordinates[0]
		}
		roadNameArray.push({
			name: pathsArray[i].properties.roadName,
			lnglat: centerPoint
		})
	}
	let tempjson = JSON.stringify({
		allPaths: pathsArray,
		maxTime,
		result,
		roadNameArray
	})
	console.log(tempjson)
	return {
		allPaths: pathsArray,
		maxTime,
		result,
		roadNameArray
	}
}
